package com.books.model

import android.annotation.SuppressLint
import android.app.Application
import android.os.Parcel
import android.os.Parcelable
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.PhoneUtils.call
import com.ceshi.network.Api
import com.facebook.stetho.common.LogUtil
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import java.lang.Appendable

class BookViewModel (appendable: Application) :AndroidViewModel(appendable){

//    //视图
//    private val _viewEvent: SharedFlowEvents<String> = SharedFlowEvents()
//    val viewEvent = _viewEvent.asSharedFlow()

    //数据  首页数据
    private val book_HomeList: MutableSharedFlow<BookHomeListModel> = MutableSharedFlow()
    val bookHomeList = book_HomeList.asSharedFlow()

//    根据书籍名称获取不同来源站
    private val book_Source: MutableSharedFlow<BookNameModel> = MutableSharedFlow()
   val bookSource = book_Source.asSharedFlow()

    //获取书籍信息
    private val book_Info: MutableSharedFlow<BookInfoModel> = MutableSharedFlow()
    val bookInfo = book_Info.asSharedFlow()


    ////获取书籍信息内容数据
    private val book_ReadData: MutableSharedFlow<BookReadDataModel> = MutableSharedFlow()
    val bookReadData = book_ReadData.asSharedFlow()



    @SuppressLint("MissingPermission")
    suspend fun getBookHomeList(category : String) {
        viewModelScope.launch {
            flow<BookHomeListModel> {
                //调用请求方法
                val data = Api.api.getBookHomeList(category)
                if (data.data != null) {
//                    _userResult.emit(data.data!!)
                    book_HomeList.emit(data)
                } else {
                   // _viewEvent.setEvent(ViewEvent.ShowToast("查询失败"))
                }
              //  _viewEvent.setEvent(ViewEvent.DismissDialog)
            }.onStart {
                // delay(2000)
               // _viewEvent.setEvent(ViewEvent.ShowDialog)
            }.catch { ex ->
                //当网络请求尚未完成，且抛出了error，则返回Error出去
             val networkResult = call(ex)
                LogUtil.d("报错$networkResult")
       //   _viewEvent.setEvent(ViewEvent.DismissDialog, ViewEvent.ShowToast("登录失败:${networkResult.second}"))
            }.collect()
        }
    }

    //根据书籍名称获取不同来源站
    suspend fun getBookNameSourceModel(name : String) {
        viewModelScope.launch {
            flow<BookNameModel> {
                //调用请求方法
                val data = Api.api.getBookName(name)
                if (data.data != null) {
//                    _userResult.emit(data.data!!)
                    book_Source.emit(data)
                } else {
                    // _viewEvent.setEvent(ViewEvent.ShowToast("查询失败"))
                }
                //  _viewEvent.setEvent(ViewEvent.DismissDialog)
            }.onStart {
                // delay(2000)
                // _viewEvent.setEvent(ViewEvent.ShowDialog)
            }.catch { ex ->
                //当网络请求尚未完成，且抛出了error，则返回Error出去
                val networkResult = call(ex)
                LogUtil.d("报错$networkResult")
                //   _viewEvent.setEvent(ViewEvent.DismissDialog, ViewEvent.ShowToast("登录失败:${networkResult.second}"))
            }.collect()
        }
    }

    //获取书籍信息
    suspend fun getBookInfo(name : String) {
        viewModelScope.launch {
            flow<BookInfoModel> {
                //调用请求方法
                val data = Api.api.getBookInfo(name)
                if (data.data != null) {
//                    _userResult.emit(data.data!!)
                    book_Info.emit(data)
                } else {
                    // _viewEvent.setEvent(ViewEvent.ShowToast("查询失败"))
                }
                //  _viewEvent.setEvent(ViewEvent.DismissDialog)
            }.onStart {
                // delay(2000)
                // _viewEvent.setEvent(ViewEvent.ShowDialog)
            }.catch { ex ->
                //当网络请求尚未完成，且抛出了error，则返回Error出去
                val networkResult = call(ex)
                LogUtil.d("报错$networkResult")
                //   _viewEvent.setEvent(ViewEvent.DismissDialog, ViewEvent.ShowToast("登录失败:${networkResult.second}"))
            }.collect()
        }
    }

    //获取书籍信息内容数据
    suspend fun getBookReadData(name : String) {
        viewModelScope.launch {
            flow<BookReadDataModel> {
                //调用请求方法
                val data = Api.api.getBookReadData(name)
                if (data.data != null) {
//                    _userResult.emit(data.data!!)
                    book_ReadData.emit(data)
                } else {
                    // _viewEvent.setEvent(ViewEvent.ShowToast("查询失败"))
                }
                //  _viewEvent.setEvent(ViewEvent.DismissDialog)
            }.onStart {
                // delay(2000)
                // _viewEvent.setEvent(ViewEvent.ShowDialog)
            }.catch { ex ->
                //当网络请求尚未完成，且抛出了error，则返回Error出去
                val networkResult = call(ex)
                LogUtil.d("报错$networkResult")
                //   _viewEvent.setEvent(ViewEvent.DismissDialog, ViewEvent.ShowToast("登录失败:${networkResult.second}"))
            }.collect()
        }
    }

    private fun call(ex: Throwable) {
        TODO("报错消息"+ex.message)
    }


}






